/*******************************************************************************
 * Copyright (c) 2011 GigaSpaces Technologies Ltd. All rights reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package org.cloudifysource.esc.installer;

import java.util.logging.Level;

import com.jcraft.jsch.Logger;

/**
 * Class to route log messages generated by JSch to Apache Commons Logging.
 * 
 * @author mrabbitt
 * @see com.jcraft.jsch.Logger
 */
public class JschJdkLogger implements Logger {

	private final java.util.logging.Logger logger;

	/**
	 * Constructor with custom category name.
	 * 
	 * @param logger
	 *            the Logger.
	 */
	public JschJdkLogger(final java.util.logging.Logger logger) {
		this.logger = logger;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jcraft.jsch.Logger#isEnabled(int)
	 */
	@Override
	public boolean isEnabled(final int level) {
		switch (level) {
		case DEBUG:
			return logger.isLoggable(Level.FINE);
		case INFO:
			return logger.isLoggable(Level.INFO);
		case WARN:
			return logger.isLoggable(Level.WARNING);
		case ERROR:
			return logger.isLoggable(Level.SEVERE);
		case FATAL:
			return logger.isLoggable(Level.SEVERE);
		default:
			return false;
		}
		
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jcraft.jsch.Logger#log(int, java.lang.String)
	 */
	@Override
	public void log(final int level, final String message) {
		switch (level) {
		case DEBUG:
			logger.fine(message);
			break;
		case INFO:
			logger.info(message);
			break;
		case WARN:
			logger.warning(message);
			break;
		case ERROR:
			logger.severe(message);
			break;
		case FATAL:
			logger.severe(message);
			break;
		default:
			logger.info(message);
			break;
		}
	}

}
